﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace XW.Utils
{
    /// <summary>
    /// 
    /// </summary>
    public class ChineseLunisolarCalendarHelper
    {
        private static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();


        #region 节气数据
        private static string[] SolarTerm = new string[] { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
        private static int[] sTermInfo = new int[] { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
        #endregion


        /// <summary>
        /// 获取农历年月日
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static ChineseDateTime GetChineseDateTime(DateTime datetime) {
            int y = cCalendar.GetYear(datetime);

            int m = cCalendar.GetMonth(datetime);

            int d = cCalendar.GetDayOfMonth(datetime);

            int leapMonth = cCalendar.GetLeapMonth(y);

            bool isLeap = false;

            if (leapMonth > 0)
            {
                if (leapMonth == m)
                {
                    isLeap = true;
                    m = m - 1;
                }
                else if (leapMonth <  m)
                {
                    m = m - 1;
                }
            }

            return new ChineseDateTime { Year = y, Month = m, Day = d, IsLeap = isLeap, SolarTerm = GetSolarTerm(datetime) };
        }
        /// <summary>
        /// 获取农历日期  （计算节日用）
        /// 这个方法所有的除夕时间都为农历12-30
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static ChineseDateTime GetChineseDateTime4Holiday(DateTime datetime)
        {
            var result = GetChineseDateTime(datetime);

            if (result.Month == 12 && result.Day == 29)
            {
                var newChDate  = GetChineseDateTime(datetime.AddDays(1));
                if (newChDate.Month == 1 && newChDate.Day == 1)
                {
                    result.Day = 30; // 如果 29 是新年  则把日改为30
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetSolarTerm(DateTime  dt) {

            DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
            DateTime newDate;
            double num;
            int y;
            string tempStr = "";

            y = dt.Year;

            for (int i = 1; i <= 24; i++)
            {
                num = 525948.76 * (y - 1900) + sTermInfo[i - 1];

                newDate = baseDateAndTime.AddMinutes(num);//按分钟计算
                if (newDate.DayOfYear == dt.DayOfYear)
                {
                    tempStr = SolarTerm[i - 1];
                    break;
                }
            }
            return tempStr;
        }
    }

    public class ChineseDateTime {

        /// <summary>
        /// 年
        /// </summary>
        public int Year { get; set; }

        /// <summary>
        /// 月
        /// </summary>
        public int Month { get; set; }

        /// <summary>
        /// 日
        /// </summary>
        public int Day { get; set; }

        /// <summary>
        /// 是否闰月
        /// </summary>
        public bool IsLeap { get; set; }

        public string SolarTerm { get; set; }
    }
}
